package web.controller;

import config.SpringContextUtil;
import entity.Department;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import service.DepartmentService;
import serviceImpl.DepartmentServiceImpl;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

@WebServlet(name = "department", urlPatterns = "/department")
public class DepartmentServlet extends HttpServlet {
    private final String department_list_page = "/WEB-INF/views/user/user_department_list.jsp";
    private final String department_edit_page = "/WEB-INF/views/user/user_department_edit.jsp";
    private final String department_add_page ="/WEB-INF/views/user/user_department_add.jsp";
    @Autowired
    private DepartmentService departmentService;

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            this.departmentService = (DepartmentServiceImpl) SpringContextUtil.getBean("departmentServiceImpl", DepartmentServiceImpl.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if (action == null) {
            List<Department> departments = departmentService.getAllDepartments();
            req.setAttribute("departments", departments);
            RequestDispatcher dispatcher = req.getRequestDispatcher(department_list_page);
            dispatcher.forward(req, resp);
        } else if (action.equals("viewEdit")) {
            String depID = req.getParameter("depID");
            req.getSession().setAttribute("depID", depID);
            Department department = departmentService.getDepartmentByDepID(depID);
            req.setAttribute("department", department);
            RequestDispatcher dispatcher = req.getRequestDispatcher(department_edit_page);
            dispatcher.forward(req, resp);
        } else if (action.equals("edit")) {
            String depID = req.getSession().getAttribute("depID").toString();
            String depName = req.getParameter("depName");
            Department department = new Department(depID, depName);
            boolean flag = departmentService.updateDepartment(department);
            if (flag) {
                String message = "修改成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/department';</script>";
                resp.getWriter().write(jsCode);
            }
        } else if (action.equals("delete")) {
            String depID = req.getParameter("depID");
            boolean flag = departmentService.deleteDepartment(depID);
            if (flag) {
                String message = "删除成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/department';</script>";
                resp.getWriter().write(jsCode);
            }
        } else if (action.equals("viewAdd")) {
            RequestDispatcher dispatcher = req.getRequestDispatcher(department_add_page);
            dispatcher.forward(req, resp);
        }else if(action.equals("add")){
            String depID = req.getParameter("depID");
            String depName = req.getParameter("depName");
            Department department = new Department(depID, depName);
            boolean flag = departmentService.addDepartment(department);
            if (flag) {
                String message = "添加成功!";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');window.location.href='" + req.getContextPath() + "/department';</script>";
                resp.getWriter().write(jsCode);
            }else {
                String message = "添加失败";
                resp.setContentType("text/html;charset=UTF-8");
                String jsCode = "<script>alert('" + message + "');</script>";
                resp.getWriter().write(jsCode);
            }
        }else if(action.equals("search")){
            String keyword = req.getParameter("keyword");
            List<Department> departments = departmentService.getDepartmentByDepNameORDepID(keyword,keyword);
            req.setAttribute("departments", departments);
            RequestDispatcher dispatcher = req.getRequestDispatcher(department_list_page);
            dispatcher.forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
